package com.manager.commons.utils;

//                   _ooOoo_    
//                  o8888888o    
//                  88" . "88    
//                  (| -_- |)    
//                   O\ = /O    
//               ____/'---'\____    
//              /. '\\| : |//' .\    
//             // \\||| : |||// \\   
//            // _|||||-:-|||||_ \\    
//           // | | \\\ - /// | | \\
//           || |_| ''\---/'' |_| || 
//            \ .-\___/'-'\___/-. /    
//          ___\. .' /--.--\ '. ./___    
//        .""'< `.___\_<|>_/___.' >'"".    
//       | |: `- \`.|'\ _ /'|.`/ -` :| |    
//        \ \ `-. \_ __\ /__ _/ .-` / /    
//======'-.___`-.___\_______/___.-`___.-'======    
//                   '=---='    
//.................................................    
//写一哈代码，你就青叫唤；信龟儿设计，你逗是傻逼。
//永无Bug，啷个可能！求佛主保佑也没球用！

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;

import com.manager.commons.file.FileUtil;
import com.manager.commons.log.LogFactory;

/**
 * CookieUtil
 * @author H
 * @version V1.0
 */
public class CookieUtil {

	protected static Logger logger = LogFactory.getLogger(FileUtil.class);

	/**
	 * 设置 Cookie, 过期时间为30分钟
	 * @param name  名称
	 * @param value 值
	 */
	public static Cookie setCookie(HttpServletResponse response, String name, String value, String path) {
		return addCookie(response, name, value, path, 60 * 30);
	}

	/**
	 * 设置 Cookie, 过期时间自定义
	 * @param name   名称
	 * @param value  值
	 * @param maxAge 过期时间, 单位秒
	 */
	public static Cookie addCookie(HttpServletResponse response, String name, String value, String path, int maxAge) {
		Cookie cookie = null;
		try {
			cookie = new Cookie(name, URLEncoder.encode(value, "UTF-8"));
			cookie.setMaxAge(maxAge);
			if (null != path) {
				cookie.setPath(path);
			}
			response.addCookie(cookie);
		} catch (UnsupportedEncodingException e) {
			logger.error(e.getMessage(), e);
		}
		return cookie;
	}

	/**
	 * 设置 Cookies, 过期时间自定义
	 * @param response 响应对象
	 * @param values   值
	 * @param path     路径
	 * @param maxAge   过期时间, 单位秒
	 * @return Cookies
	 */
	public static ArrayList<Cookie> addCookies(HttpServletResponse response, Map<String, String> values, String path, int maxAge) {
		Set<Map.Entry<String, String>> entries = values.entrySet();
		ArrayList<Cookie> cookies = new ArrayList<>();
		try {
			for (Map.Entry<String, String> entry : entries) {
				Cookie cookie = new Cookie(entry.getKey(), URLEncoder.encode(entry.getValue(), "UTF-8"));
				cookie.setMaxAge(maxAge);
				if (null != path) {
					cookie.setPath(path);
				}
				response.addCookie(cookie);
				cookies.add(cookie);
			}
		} catch (UnsupportedEncodingException e) {
			logger.error(e.getMessage(), e);
		}
		return cookies;
	}

	/**
	 * 获得指定Cookie的值
	 * @param name 名称
	 * @return 值
	 */
	public static String getCookie(HttpServletRequest request, String name) {
		return getCookie(request, null, name, false);
	}

	/**
	 * 获得指定Cookie的值，并删除。
	 * @param name 名称
	 * @return 值
	 */
	public static String getCookie(HttpServletRequest request, HttpServletResponse response, String name) {
		return getCookie(request, response, name, true);
	}

	/**
	 * 获得指定Cookie的值
	 * @param request   请求对象
	 * @param response  响应对象
	 * @param name      名字
	 * @param isRemoved 是否移除
	 * @return 值
	 */
	public static String getCookie(HttpServletRequest request, HttpServletResponse response, String name, boolean isRemoved) {
		String value = null;
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals(name)) {
					try {
						value = URLDecoder.decode(cookie.getValue(), "UTF-8");
					} catch (UnsupportedEncodingException e) {
						logger.error(e.getMessage(), e);
					}
					if (isRemoved) {
						cookie.setMaxAge(0);
						response.addCookie(cookie);
					}
					return value;
				}
			}
		}
		return value;
	}
}